/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.File.MappingFileReader;
import model.File.QueryFileReader;
import model.File.QueryReformulation;
import model.entities.Dataset;
import model.mapping.Mapping;
import model.query.QueryGraph;
import test.QueryReformulationTest;

/**
 *
 * @author nusa
 */
public class mainJFrame extends javax.swing.JFrame {

    public mainJFrame() {
        initComponents();
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelWelcome = new javax.swing.JLabel();
        jLabelSparqlQuery = new javax.swing.JLabel();
        RewriteButton = new javax.swing.JButton();
        ClearButton = new javax.swing.JButton();
        SaveButtonQuery = new javax.swing.JButton();
        ResultQuery = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jTextAreaSparqlQuery = new javax.swing.JTextArea();
        jScrollPaneResults = new javax.swing.JScrollPane();
        jTextAreaResults = new javax.swing.JTextArea();
        ClearButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        MenuMain = new javax.swing.JMenu();
        MenurSair = new javax.swing.JMenuItem();
        MenuMappings = new javax.swing.JMenu();
        ClassMappingMenu = new javax.swing.JMenuItem();
        PropertyMapping = new javax.swing.JMenuItem();
        MenuHelp = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OLinDa");
        setBackground(new java.awt.Color(94, 92, 92));
        setResizable(false);

        jLabelWelcome.setText("Welcome to OLinDa");

        jLabelSparqlQuery.setText("SPARQL Query");

        RewriteButton.setText("Decomposer");
        RewriteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RewriteButtonActionPerformed(evt);
            }
        });

        ClearButton.setText("Clear");
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

        SaveButtonQuery.setText("Save");
        SaveButtonQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonQueryActionPerformed(evt);
            }
        });

        ResultQuery.setText("Results");

        jTextAreaSparqlQuery.setColumns(20);
        jTextAreaSparqlQuery.setRows(5);

        jTextAreaResults.setColumns(20);
        jTextAreaResults.setRows(5);
        jScrollPaneResults.setViewportView(jTextAreaResults);

        ClearButton2.setText("Clear");
        ClearButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButton2ActionPerformed(evt);
            }
        });

        MenuMain.setText("Menu");

        MenurSair.setText("Exit");
        MenurSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenurSairActionPerformed(evt);
            }
        });
        MenuMain.add(MenurSair);

        jMenuBar1.add(MenuMain);

        MenuMappings.setText("Mappings");

        ClassMappingMenu.setText("Class Mapping");
        ClassMappingMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClassMappingMenuActionPerformed(evt);
            }
        });
        MenuMappings.add(ClassMappingMenu);

        PropertyMapping.setText("Property Mapping");
        PropertyMapping.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PropertyMappingActionPerformed(evt);
            }
        });
        MenuMappings.add(PropertyMapping);

        jMenuBar1.add(MenuMappings);

        MenuHelp.setText("Help");
        jMenuBar1.add(MenuHelp);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparator3)
                        .addGap(12, 12, 12))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jTextAreaSparqlQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 474, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(RewriteButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ClearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(SaveButtonQuery, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(75, 75, 75))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ResultQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(430, 430, 430)
                                .addComponent(ClearButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(50, 50, 50))
                            .addComponent(jScrollPaneResults, javax.swing.GroupLayout.PREFERRED_SIZE, 654, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelSparqlQuery))
                        .addContainerGap(20, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(261, 261, 261)
                .addComponent(jLabelWelcome)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelWelcome, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jLabelSparqlQuery)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextAreaSparqlQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ResultQuery)
                            .addComponent(ClearButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPaneResults, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(RewriteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ClearButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SaveButtonQuery)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        getAccessibleContext().setAccessibleName("OLinDa Query Rewriting");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MenurSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenurSairActionPerformed
        System.exit(0);
    }//GEN-LAST:event_MenurSairActionPerformed

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        jTextAreaSparqlQuery.setText(null);
    }//GEN-LAST:event_ClearButtonActionPerformed

    private void RewriteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RewriteButtonActionPerformed
       
        String textSparqlQuery = jTextAreaSparqlQuery.getText();
        try{
            try (FileWriter querySparql = new FileWriter(new File("query.txt"),false)) {
                PrintWriter out = new PrintWriter(querySparql);  
                out.printf(textSparqlQuery);
                out.close();
                querySparql.close();
            }  
        } catch (IOException ex) {
            Logger.getLogger(mainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        //QueryReformulationTest.queryRewriting();
        //chamar main queryreformulationtest
        Dataset dataset = new Dataset("Dataset1", "http://127.0.0.1:2020/dataset1");
        String queryFile = "query.txt";
	QueryFileReader queryFileReader = new QueryFileReader();
	QueryGraph queryGraph = queryFileReader.getQueryFromFile(queryFile, dataset);
		
	String predicateMappingFile = "predicateMapping.txt";
	String classmappingFile = "classMapping.txt";
	MappingFileReader mappingFileReader = new MappingFileReader();
	List<Mapping> predicateMappings = mappingFileReader.getMappingsFromFileProperty(predicateMappingFile, dataset);
	List<Mapping> classMappings = mappingFileReader.getMappingsFromFileClass(classmappingFile, dataset);
		
	QueryReformulation queryReformulation = new QueryReformulation();
	List<QueryGraph> subQueriesGraph = queryReformulation.reformulateQuery(queryGraph, predicateMappings, classMappings);
        
        String datasetResult = null;
        String select = null;
        QueryGraph where = null;
        
        for(QueryGraph subQuery : subQueriesGraph){
             jTextAreaResults.append("Subquery for dataset: " +subQuery.getDataset().getName()+ "\n"
                    + queryGraph.getSelect() + 
                   "\nWHERE {\n" +
			subQuery +
			"}\n\n");   
 
        }
    
        
         
    }//GEN-LAST:event_RewriteButtonActionPerformed

    private void SaveButtonQueryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonQueryActionPerformed
              //salvar query no txt
        String textSparqlQuery = jTextAreaSparqlQuery.getText();
        try{
            try (FileWriter querySparql = new FileWriter(new File("query.txt"),false)) {
                PrintWriter out = new PrintWriter(querySparql);  
                out.printf(textSparqlQuery);
                out.close();
                querySparql.close();
            }  
        } catch (IOException ex) {
            Logger.getLogger(mainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "The query it was saved!!!");
        
  
    }//GEN-LAST:event_SaveButtonQueryActionPerformed

    private void ClassMappingMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClassMappingMenuActionPerformed
        new ClassMappingGUI().setVisible(true);
    }//GEN-LAST:event_ClassMappingMenuActionPerformed

    private void PropertyMappingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PropertyMappingActionPerformed
        new PropertyMappingGUI().setVisible(true);
    }//GEN-LAST:event_PropertyMappingActionPerformed

    private void ClearButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButton2ActionPerformed
        jTextAreaResults.setText(null);
    }//GEN-LAST:event_ClearButton2ActionPerformed

    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mainJFrame().setVisible(true);
                
            }
            
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ClassMappingMenu;
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton ClearButton2;
    private javax.swing.JMenu MenuHelp;
    private javax.swing.JMenu MenuMain;
    private javax.swing.JMenu MenuMappings;
    private javax.swing.JMenuItem MenurSair;
    private javax.swing.JMenuItem PropertyMapping;
    private javax.swing.JLabel ResultQuery;
    private javax.swing.JButton RewriteButton;
    private javax.swing.JButton SaveButtonQuery;
    private javax.swing.JLabel jLabelSparqlQuery;
    private javax.swing.JLabel jLabelWelcome;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPaneResults;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextArea jTextAreaResults;
    private javax.swing.JTextArea jTextAreaSparqlQuery;
    // End of variables declaration//GEN-END:variables
}
